package com.honey.mybatis.generator.model.method;

import com.honey.compilation.java.Block;
import com.honey.compilation.java.Clazz;
import com.honey.compilation.java.ElseIfBlock;
import com.honey.compilation.java.IfBlock;
import com.honey.compilation.java.JavaKeyWord;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.Parameter;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.dbmapping.structure.Column;
import com.honey.core.types.ExceptionFullyQualifiedJavaType;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.types.JDKFullyQualifiedJavaType;
import com.honey.mybatis.generator.AbstractMethod;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.model.ModelConstant;

public class SetParameterMethod extends AbstractMethod {
	
	private static final  String VARIABLE_PS="ps";
	
	private static final  String VARIABLE_INDEX="index";
	
	private static final  String VARIABLE_PARAMETER="parameter";
	
	private static final  String VARIABLE_JDBC_TYPE="jdbcType";
	
	private static final  FullyQualifiedJavaType[] EXCEPTION = new FullyQualifiedJavaType[]{
		ExceptionFullyQualifiedJavaType.getSQLExceptionInstance(),
	}; 
	
	private static final Parameter []PARAMETER =new Parameter[]{ 
		
		new Parameter(ModelConstant.PREPARED_STATEMENT,VARIABLE_PS),
		
		new Parameter(JDKFullyQualifiedJavaType.getIntInstance(),VARIABLE_INDEX),
		
		new Parameter(JDKFullyQualifiedJavaType.getObjectInstance(),VARIABLE_PARAMETER),
		
		new Parameter(ModelConstant.IBATIS_JDBCTYPE,VARIABLE_JDBC_TYPE),
	};
	
	private static final String[] JAVA_DOC=new String[]{
		"myBatis用户定义的类型转换，把定义的类型值传入数据库的PreparedStatement对象中。"
	};
	
	private static final String[] ANNOTATION = new String[]{
		"Override",
	};
	
	public SetParameterMethod(Clazz clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#addBody(com.honey.compilation.java.Method)
	 */
	@Override
	protected void addBody(Method method) {
		method.addBodyLine( 
			new StringBuilder()
			.append(JavaKeyWord.IF).append(JavaKeyWord.OPERATION_BRACKET_LEFT)
			.append(VARIABLE_PARAMETER).append(JavaKeyWord.LOGIC_EQUALS).append(JavaKeyWord.NULL.getValue()).append(JavaKeyWord.LOGIC_OR)
			.append(VARIABLE_JDBC_TYPE).append(JavaKeyWord.LOGIC_EQUALS).append(JavaKeyWord.NULL.getValue())
			.append(JavaKeyWord.OPERATION_BRACKET_RIGHT).append(JavaKeyWord.OPERATION_BRACE_LEFT)
			.append(VARIABLE_PS).append(".setNull").append(JavaKeyWord.OPERATION_BRACKET_LEFT).append(VARIABLE_INDEX).append(", ").append( ModelConstant.IBATIS_JDBCTYPE.getShortName() ).append(".OTHER.TYPE_CODE) ; return ;}")
			.toString()
		);
		//System.out.println(sb);
		//method.addBodyLine("if( parameter == null || jdbcType == null){ ps.setNull(i, JdbcType.OTHER.TYPE_CODE) ; return ;}");
		boolean bool = true ;
		String entityName =super.getModelPrimitiveClassName(introspectSchema.getSchema());
		for(Column column : introspectSchemaColumn.allColumns()){
			if( column.getEnumSchema() != null ){
				
				String enumName =super.getStateEnumName(column.getEnumSchema());
				Block block =null ;
				if(bool){
					block = new IfBlock(VARIABLE_PARAMETER + " instanceof "+entityName+"."+enumName);
					bool = false ;
				}else{
					block = new ElseIfBlock(VARIABLE_PARAMETER + " instanceof "+entityName+"."+enumName);
				}
				
				block.addBodyLine("Object value = (("+entityName+"."+enumName+")"+VARIABLE_PARAMETER+").getValue()");
				block.addBodyLine(VARIABLE_PS+".setObject("+VARIABLE_INDEX+", value)");
				//block.addBodyLine("return");
				method.addBlocks(block);
			}
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodName()
	 */
	@Override
	protected String getMethodName() {

		return ModelConstant.SET_PARAMETER_METHOD;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getParameter()
	 */
	@Override
	protected Parameter[] getParameter() {
		return PARAMETER;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getJavaDoc()
	 */
	@Override
	protected String[] getJavaDoc() {
		return JAVA_DOC;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getAnnotation()
	 */
	@Override
	protected String[] getAnnotation() {
		return ANNOTATION;
	}

	@Override
	protected FullyQualifiedJavaType[] getException() {
		return EXCEPTION;
	}

	
}
